<template>
  <MesBox v-model="visible" :title="Type" :width="1000" :height="400">
    <MesForm
      ref="myform"
      :formFields="formFields"
      :formRules="formRules"
    ></MesForm>
    <template #footer>
      <el-button type="danger" @click="saveData">
        <i class="el-icon-check"></i>
        保存</el-button
      >
    </template>
  </MesBox>
</template>

<script setup>
import MesBox from '@/components/basic/MesBox.vue'
import MesForm from '@/components/basic/MesForm.vue'
import { ref, getCurrentInstance } from 'vue'
import { getNowDate } from '@/uitils/dateFormatUtil'

const emit = defineEmits(['parentCall'])
const that = getCurrentInstance().appContext.config.globalProperties
const myform = ref(null)
let visible = ref(false)
let formFields = ref({
  WoCode: '',
  ProductName: '',
  ProductCode: '',
  ProductStandard: '',
  Process_Id: null,
  StartTime: '',
  EndTime: '',
  Id: '',
})
let formRules = ref([])
let Type = ref('')

const saveData = () => {
  myform.value.validate(async (valid) => {
    if (valid) {
      let res = null
      if (Type.value == '生成返工工单') {
        res = await that.http.post(
          '/api/Production_WorkOrder/CreateReFromWorkOrder',
          formFields.value,
          true
        )
      } else {
        res = await that.http.post(
          '/api/Production_WorkOrder/CreateReScrWorkOrder',
          formFields.value,
          true
        )
      }

      if (res.status) {
        that.$message.success('创建成功')
        visible.value = false
        emit('parentCall', ($parent) => {
          $parent.search()
        })
      } else {
        that.$message.error(res.message)
      }
    }
  })
}

const open = (row, type) => {
  Type.value = type
  if (type == '生成返工工单') {
    formRules.value = [
      [
        {
          title: '工单号',
          field: 'WoCode',
          readonly: true,
        },
        {
          title: '产品名称',
          field: 'ProductName',
          readonly: true,
        },
      ],
      [
        {
          title: '产品编码',
          field: 'ProductCode',
          readonly: true,
        },
        {
          title: '规格型号',
          field: 'ProductStandard',
          readonly: true,
        },
      ],
      [
        {
          title: '工艺路线',
          field: 'Process_Id',
          required: true,
          dataKey: 'reworkProcessList',
          type: 'select',
        },
        {
          title: '计划开始时间',
          field: 'StartTime',
          required: true,
          type: 'datetime',
        },
      ],
      [
        {
          title: '计划结束时间',
          field: 'EndTime',
          required: true,
          type: 'datetime',
        },
        {
          title: '工单编号',
          field: 'Id',
          hidden: true,
          readonly: true,
        },
      ],
    ]
  } else {
    formRules.value = [
      [
        {
          title: '工单号',
          field: 'WoCode',
          readonly: true,
        },
        {
          title: '产品名称',
          field: 'ProductName',
          readonly: true,
        },
      ],
      [
        {
          title: '产品编码',
          field: 'ProductCode',
          readonly: true,
        },
        {
          title: '规格型号',
          field: 'ProductStandard',
          readonly: true,
        },
      ],
      [
        {
          title: '计划开始时间',
          field: 'StartTime',
          required: true,
          type: 'datetime',
        },
        {
          title: '计划结束时间',
          field: 'EndTime',
          required: true,
          type: 'datetime',
        },
      ],
      [
        {
          title: '工单编号',
          field: 'Id',
          hidden: true,
          readonly: true,
        },
      ],
    ]
  }

  formFields.value.Id = row.WorkOrder_Id
  formFields.value.WoCode = row.WoCode
  formFields.value.ProductName = row.ProductName
  formFields.value.ProductCode = row.ProductCode
  formFields.value.ProductStandard = row.ProductStandard
  formFields.value.StartTime = getNowDate()
  visible.value = true
}

defineExpose({ open })
</script>

<style lang="less" scoped></style>
